package com.kpmg.datalake.common.utils;

import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.exception.BusinessException;

import java.util.Collection;
import java.util.Map;

/**
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-07-19 23:40:38
 */
public class AssertUtil {
    /**
     * 静态方法，防止实例化
     */
    protected AssertUtil() {

    }

    /**
     * 断言判断只能为真
     *
     * @param condition 断言条件
     * @param msg       提示信息
     */
    public static void assertTrue(boolean condition, String msg) {
        if (!condition) {
            throw new BusinessException(ResultCodeEnum.CODE4003, msg);
        }
    }

    /**
     * 断言判断只能为真
     *
     * @param conditon 断言条件
     */
    public static void assertTrue(boolean conditon) {
        assertTrue(conditon, ResultCodeEnum.CODE4003.getDisplay());
    }

    /**
     * 断言判断只能为假
     *
     * @param condition 判断条件
     * @param msg       提示信息
     */
    public static void assertFalse(boolean condition, String msg) {
        if (condition) {
            throw new BusinessException(ResultCodeEnum.CODE4004, msg);
        }
    }

    /**
     * 断言判断只能为假
     *
     * @param condition 判断条件
     */
    public static void assertFalse(boolean condition) {
        assertFalse(condition, ResultCodeEnum.CODE4004.getDisplay());
    }

    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     */
    public static void assertNotEmpty(String condition, String msg) {
        if (condition == null || condition.trim().length() == 0) {
            throw new BusinessException(ResultCodeEnum.CODE4000, msg);
        }
    }


    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     */
    public static void assertNotEmpty(String condition) {
        assertNotEmpty(condition, ResultCodeEnum.CODE4000.getDisplay());
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     */
    public static void assertIsEmpty(String condition, String msg) {
        if (condition != null && condition.trim().length() > 0) {
            throw new BusinessException(ResultCodeEnum.CODE4001, msg);
        }
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     */
    public static void assertIsEmpty(String condition) {
        assertIsEmpty(condition, ResultCodeEnum.CODE4001.getDisplay());
    }

    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     */
    public static void assertNotEmpty(Number condition, String msg) {
        if (condition == null) {
            throw new BusinessException(ResultCodeEnum.CODE4000, msg);
        }
    }


    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     */
    public static void assertNotEmpty(Number condition) {
        assertNotEmpty(condition, ResultCodeEnum.CODE4000.getDisplay());
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     */
    public static void assertIsEmpty(Number condition, String msg) {
        if (condition != null) {
            throw new BusinessException(ResultCodeEnum.CODE4001, msg);
        }
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     */
    public static void assertIsEmpty(Number condition) {
        assertIsEmpty(condition, ResultCodeEnum.CODE4001.getDisplay());
    }


    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     * @param <E>       type
     */
    public static <E> void assertNotEmpty(Collection<E> condition, String msg) {
        if (condition == null || condition.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.CODE4000, msg);
        }
    }


    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     * @param <E>       type
     */
    public static <E> void assertNotEmpty(Collection<E> condition) {
        assertNotEmpty(condition, ResultCodeEnum.CODE4000.getDisplay());
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     * @param <E>       type
     */
    public static <E> void assertIsEmpty(Collection<E> condition, String msg) {
        if (condition != null && !condition.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.CODE4001, msg);
        }
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     * @param <E>       type
     */
    public static <E> void assertIsEmpty(Collection<E> condition) {
        assertIsEmpty(condition, ResultCodeEnum.CODE4001.getDisplay());
    }


    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     * @param <K,V>     type
     */
    public static <K, V> void assertNotEmpty(Map<K, V> condition, String msg) {
        if (condition == null || condition.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.CODE4000, msg);
        }
    }


    /**
     * 断言判断不能为空
     *
     * @param condition 断言条件
     * @param <K,V>     type
     */
    public static <K, V> void assertNotEmpty(Map<K, V> condition) {
        assertNotEmpty(condition, ResultCodeEnum.CODE4000.getDisplay());
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     * @param msg       提示信息
     * @param <E>       type
     */
    public static <K, V> void assertIsEmpty(Map<K, V> condition, String msg) {
        if (condition != null && !condition.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.CODE4001, msg);
        }
    }


    /**
     * 断言判断能为空
     *
     * @param condition 断言条件
     * @param <K,V>     type
     */
    public static <K, V> void assertIsEmpty(Map<K, V> condition) {
        assertIsEmpty(condition, ResultCodeEnum.CODE4001.getDisplay());
    }

}
